<?php

namespace App\Models;

class JifenProduct extends Base
{

    //分页获取列表数据
    public function getPageList($options=[],$where=[]){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = $this->where($where)->orderBy('created_at','desc');
            $result = $this->paging($model,$page,$limit);
            return $result;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //新增单个商品
    public function oneInsert($data=array()){
        try {
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
            $id = $this->insertGetId($data);
            if($id){
                return [
                    'code'=>0,
                    'data'=>$id
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'添加失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //编辑单个商品
    public function doUpdate($data){
        try {
            $now = date('Y-m-d H:i:s');
            $data['updated_at'] = $now;
            $res = $this->where('id',$data['id'])->update($data);
            if($res){
                return [
                    'code'=>0,
                    'data'=>$data['id']
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'更新失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //改变商品状态
    public function changeStatus($data){
        try {
            //存储数据
            $model = self::find($data['id']);
            if($data['status']){
                $model->deleted_at = null;
            }else{
                $model->deleted_at = date('Y-m-d H:i:s');
            }
            $res = $model->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>$model->id
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>'操作失败'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //设置商品图片
    public function setProdImgs($data){
        try {
            $model = new Image();
            $result =  $model->where([
                ['rid','=',$data['proid']],['type','=',2]
            ])->delete();
            if($result===false){
                return [
                    'code'=>1,
                    'message'=>'操作失败,请重试'
                ];
            }else{
                $levs = $data['images'];
                $insert = [];
                $now = date('Y-m-d H:i:s');
                $title = $this->select('proname','proname_zn')->find($data['proid']);
                foreach ($levs as $val){
                    $insert[]=['type'=>2,'rid'=>$data['proid'],'src'=>$val,'title'=>$title->proname,'title_zn'=>$title->proname_zn,'created_at'=>$now,'updated_at'=>$now];
                }
                $res = $model->insert($insert);
                if($res){
                    return [
                        'code'=>0,
                        'data'=>''
                    ];
                }else{
                    return [
                        'code'=>1,
                        'message'=>'操作失败'
                    ];
                }
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //获取分页列表
    public function getList($model,$page,$limit,$lang='zn'){

    }
}
